/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.teotigraphix.dx.patterns.manager.toolkit
{

import flash.utils.Dictionary;

import mx.events.PropertyChangeEvent;

import org.teotigraphix.dx.core.ToolKitConstants;
import org.teotigraphix.dx.core.ToolKitHelper;
import org.teotigraphix.dx.interfaces.IToolKitComponent;

[ExcludeClass]

/**
 * @private
 */
public class InvalidPropertyCollection
{
	include "../../../core/Version.as"
	
	private var map:Dictionary = new Dictionary(true);
	
	public function InvalidPropertyCollection()
	{
	}
	
	/**
	 * @private
	 */
	private function createInvalidPropertyInfo(event:PropertyChangeEvent):InvalidProperty
	{
		var o:InvalidProperty = new InvalidProperty();
		o.component = event.source as IToolKitComponent;
		o.newValue = event.newValue;
		o.oldValue = event.oldValue;
		o.name = event.property.toString();
		return o;
	}	
	
	
	
	public function propertyChanged(event:PropertyChangeEvent):void
	{
		var client:IToolKitComponent = event.source as IToolKitComponent;
		if (!hasItem(client))
			return;
		
		addInvalidProperty(client, event);
	}	
	
	public function validate(client:IToolKitComponent):void
	{
		if (!hasItem(client))
			return;
		
		var o:InvalidProperty;
		
		for each(var name:String in ToolKitConstants.toolKitPropertyMap)
		{
			// this could have been deleted in the validations
			if (!hasItem(client))
				return;
			
			o = removeInvalidProperty(client, name);
			if (o)
			{
				var first:String = name.substr(0, 1);
				var nname:String = name.substr(1, name.length - 1);
				nname = first.toUpperCase() + nname;
				ToolKitHelper["set" + nname](client, o.newValue, o.oldValue);
			}
		}
		
		clearItem(client);
	}	
	
	public function hasItem(client:IToolKitComponent):Boolean
	{
		return (map[client] != null);
	}
	
	public function getItem(client:IToolKitComponent):Object
	{
		return map[client];
	}	
	
	public function addItem(client:IToolKitComponent, item:Object):void
	{
		map[client] = item;
	}
	
	public function clearItem(client:IToolKitComponent):void
	{
		map[client] = {};
	}	
	
	public function removeItem(client:IToolKitComponent):Object
	{
		var item:Object;
		if (hasItem(client))
		{
			item = map[client];
			delete map[client];
		}
		return item;
	}
	
	public function getInvalidProperty(client:IToolKitComponent, name:String):InvalidProperty
	{
		var item:InvalidProperty;
		if (hasItem(client))
		{
			item = map[client][name];
		}
		return item;
	}
	
	public function addInvalidProperty(client:IToolKitComponent, event:PropertyChangeEvent):void
	{
		var info:InvalidProperty = createInvalidPropertyInfo(event);
		if (hasItem(client))
		{
			map[client][info.name] = info;
		}
	}
	
	public function removeInvalidProperty(client:IToolKitComponent, name:String):InvalidProperty
	{
		var item:InvalidProperty;
		if (hasItem(client))
		{
			item = map[client][name];
			delete map[client][name];
		}
		return item;
	}	
}
}